/*
 * Copyright (c) 2020 pig4cloud Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.bblocks.auth.support.service.impl;

import com.bblocks.auth.support.service.AppUserDemoDetailsService;
import com.bblocks.enums.AppTypeEnum;
import com.bblocks.enums.LoginTypeEnum;
import com.bblocks.pojo.ComUser;
import com.bblocks.util.R;
import com.pig4cloud.pig.admin.api.dto.UserInfo;
import com.pig4cloud.pig.admin.api.feign.RemoteUserService;
import com.pig4cloud.pig.common.core.constant.CacheConstants;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

/**
 * 用户登录名 详细信息
 *
 * @author Zhao
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class AppDemoUserDetailsServiceImpl extends AppUserDemoDetailsService {

	private final CacheManager cacheManager;
	private final RemoteUserService remoteUserService;

	/**
	 * 用户密码登录
	 * @param username 用户密码登录
	 * @return
	 */
	@Override
	@SneakyThrows
	public UserDetails loadUserByUsername(String username) {
		R<UserInfo> result = remoteUserService.info(username);
		UserDetails userDetails = getUserDetails(result);
		return userDetails;
	}

	@Override
	public UserDetails loadUserByUser(ComUser pigxUser) {
		return pigxUser;
	}




	@Override
	public int getOrder() {
		return 10;
	}

	/**
	 * 支持所有的 mobile 类型
	 * @param clientId 目标客户端
	 * @param grantType 授权类型
	 * @return true/false
	 */
	@Override
	public boolean support(String clientId, String grantType, AppTypeEnum apptype) {
		return  LoginTypeEnum.Psw.getName().equals(grantType) && this.appType() == apptype;
	}

}
